<?php 
// $this->categories['0'] = "None";
 ?>
<div class="breadLine">
	<ul class="breadcrumb">
		<li><a href="<?php echo $this->adminURL('index', 'index') ?>">Dashboard</a> <span class="divider">&gt;</span></li>                
		<li class="active">Danh sách danh mục</li>
	</ul>
</div>
<div class="workplace">
<div class="table-sorting">
<table width=100% class="table ">
	<thead>
	<tr>
	<th width=50>#</th>
	<th width=380>Title</th>
	<th>Alias</th>
	<th width=100>Act</th>
	</tr>
	</thead>
	<form action="<?php echo $this->adminURL('category', 'list') ?>" method="get" id="frmSearch">
	<tr class="search">
	<td>&nbsp;</td>
	<td>
		<input type="text" placeholder="Tiêu đề" name="q" value="<?php echo $this->q ; ?>"/>
	</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	</tr>
	</form>
	<?php 
	$count =1;
if($this->categories) foreach ($this->categories as $cate){ ?>

<tr>
	<td><?php echo $count++; ?></td>
	<td><span data-id="<?php echo $cate['cat_id'];?>" class="editinline"><?php echo $cate['cat_title'];?></span></td>
	<td><?php echo $cate['cat_alias'];?></td>
	<td>
	<div class="btn-group">
		<a href="<?php 
		echo $this->adminURL('category', 'edit', $cate['cat_id']);
		?>" class="btn btn-small btn-primary"><i class="icon-pencil"></i></a>
		<a href="<?php 
		echo $this->adminURL('category', 'delete', $cate['cat_id']);
		?>" class="btn btn-small btn-danger delete"><i class="icon-remove "></i></a>
	</div>
	</td>
</tr>

<div style="display:none;" id="templateEditTitle">
	<div class="input-append">
		<input type="text" name="cat_title" value="" class="cat_title"/>
		<button type="button" class="btn btn-change">OK
		</button>
	</div>
</div>

<?php 
}
	?>
	
</table>
</div>
</div> <!-- end of .workplace -->

<script type="text/javascript">
$(function (){
	
	$('.editinline').click(function(){
		
		var dis = $(this);
		var id = dis.attr('data-id');
		html_form = $('#templateEditTitle').html();
		
		dis.hide('fast');
		dis.after(html_form);
		
		dis.next().find('input[type=text]').val(dis.text());
		dis.next().find('button').click(function(){
			var val = dis.next().find('input[type=text]').val();
			$.post ('<?php echo $this->adminURL('category', 'change'); ?>',
			{
				cat_id : id,
				cat_title : val
			}, function(respone){
				console.log(respone);
				dis.text(val).show('fast');
				dis.next().remove();
			});
			
		});
		
		console.log('editinline clicked ' + id);
		
		
	});

	$('.delete').click(function(){
		console.log('Delete clicked');
		if (!confirm ('Bạn có muốn xóa ?')) {
			return false;
		}
		var _dis = this;
		var _id = $(this).attr('data-id');
		$.post('<?php echo $this->baseUrl() ?>/admin/category/delete', {
			id : _id
			},
			function(respone){
				console.log(respone);
				$(_dis).parent().parent().parent().remove();
			}
		);
		
		
		return false;
	});
	$('#cate').change(function (){
	$('#frmSearch').submit();
	
	});
	$('#change').submit(function(){return false;});
});
</script>